<?php
class article extends core {
	public function __construct() {
		parent::__construct();
	}

	//index
	public function index() {
		$where = '';
		$id = (isset($_GET['id']) && trim($_GET['id'])) ? intval($_GET['id']) : '';
		$keyword = (isset($_GET['keyword']) && trim($_GET['keyword'])) ? trim($_GET['keyword']) : '';
		if ($keyword) {
			$where .= "AND (title LIKE '%{$keyword}%' OR content LIKE '%{$keyword}%')";
		}
		if ($id) {
			$where .= "AND id='{$id}'";
		}
		//分页
		$this->setpages(array('id'=>$id, 'keyword'=>$keyword));

		$sql = "SELECT  *  FROM jf_article WHERE 1 {$where} ORDER BY id DESC";
		$rs = $this->ezr->get_results($sql);
		$nav = $this->ezr->get_navigation();
		$this->smarty->assign('rs',$rs);
		$this->smarty->assign('page',$nav);

		$this->smarty->assign('is_admin', $_SESSION['admin']->is_admin);
		$this->display();
	}

	public function add() {
		if (IS_POST) {
			$this->save();
		} else {
			$this->display();
		}
	}	

	//add
	public function edit() {
		if (IS_POST) { //添加
			$this->save();
		} else { //显示
			$id = (isset($_GET['id']) && trim($_GET['id'])) ? intval($_GET['id']) : 0;
			$sql = "SELECT a.* ,p.pic ,p.memo , p.sort as sort_pic FROM jf_article a LEFT JOIN jf_article_pic p 
					ON a.id = p.article_id  WHERE a.id='{$id}'";
			$row = $this->db->get_row($sql);
			$this->smarty->assign('row', $row);
			
			$sql = "SELECT * FROM jf_article_pic WHERE article_id='{$id}'";
			$pics = $this->db->get_results($sql);
			$this->smarty->assign('pics', $pics);
			
			$sql = "SELECT g.* FROM jf_goods g LEFT JOIN jf_article_goods ag ON ag.goods_id=g.id WHERE ag.article_id='{$id}'";
			$goods = $this->db->get_results($sql);
			$this->smarty->assign('goods', $goods);
			
			$this->display();
		}
	}

	public function upload_pic(){
        global $domain;
        $file_result = upload_one_file('article','pic_file',true);
        $file_result = $file_result['file'];
        echo $file_result;
    }
	
	//写入到数据库
	public function save() {
		$id = (isset($_POST['id']) && intval($_POST['id'])) ? intval($_POST['id']) : 0;
		$type_id = (isset($_POST['type_id']) && trim($_POST['type_id'])) ? intval($_POST['type_id']) : 0;
		$title = (isset($_POST['title']) && trim($_POST['title'])) ? trim($_POST['title']) : '';
		$content = (isset($_POST['content']) && trim($_POST['content'])) ? trim($_POST['content']) : '';
		$memo = (isset($_POST['memo']) && trim($_POST['memo'])) ? trim($_POST['memo']) : '';
		$status = (isset($_POST['status']) && intval($_POST['status'])) ? intval($_POST['status']) : 0;
		$sort = (isset($_POST['sort']) && intval($_POST['sort'])) ? intval($_POST['sort']) : 0;
		$pics = isset($_POST['pics']) ? $_POST['pics'] : '';
		$sort_pic = isset($_POST['sort_pic']) ? $_POST['sort_pic'] : '';
		$goods = isset($_POST['goods']) ? $_POST['goods'] : '';
		// $pic = upload_one_file("acticle","pic");
		if ($id > 0) {  //edit
			$sql = "UPDATE jf_article SET title='{$title}', content='{$content}', sort='{$sort}' , 
					status='{$status}', type_id='{$type_id}'  
					 WHERE id='{$id}'";
			$this->db->query($sql);
		} else { //add
			$sql = "INSERT INTO jf_article(title, content, sort, type_id, add_time, status) 
					VALUES('{$title}', '{$content}', '{$sort}', '{$type_id}', '".time()."', '{$status}')";
			$this->db->query($sql);
			$id = $this->db->insert_id;
		}
		
		//文章图片
		if (is_array($pics)) {
			$sql = "DELETE FROM jf_article_pic WHERE article_id='{$id}'";
			$this->db->query($sql);
			$sql = "INSERT INTO jf_article_pic(article_id, pic, add_time, sort) VALUES ";
			for ($i=0; $i<count($pics); $i++) {
				$pic = $pics[$i];
				$sort = 999;
				if (is_array($sort_pic) && isset($sort_pic[$i])) $sort = $sort_pic[$i];
				$sql .= "('{$id}', '{$pic}', '".time()."', '{$sort}'),";
			}
			$sql = trim($sql, ',');
			$this->db->query($sql);
		}
		
		//关联商品
		if (is_array($goods)) {
			$sql = "DELETE FROM jf_article_goods WHERE article_id='{$id}'";
			$this->db->query($sql);
			$sql = "INSERT INTO jf_article_goods(article_id, goods_id) VALUES ";
			for ($i=0; $i<count($goods); $i++) {
				$sql .= "('{$id}', '".$goods[$i]."'),";
			}
			$sql = trim($sql, ',');
			$this->db->query($sql);
		}
		
		header("Location:?app=article&act=index");
	}

	//delete
	public function delete() {
		$id = (isset($_GET['id']) && trim($_GET['id'])) ? intval($_GET['id']) : 0;
		$sql = "DELETE FROM jf_article WHERE id='{$id}'";
		if($this->db->query($sql)){	
			$sql = "DELETE FROM jf_article_pic WHERE article_id='{$id}'";
			$this->db->query($sql);
			//删除该商店被收藏
			$sql="DELETE FROM jf_favorite WHERE item_id='{$id}' AND type_id = 4";
			$this->db->query($sql);
		}
		header("Location:?app=article&act=index");
	}
	
	//like
	public function likes(){
		$where = '';
		$article_id = (isset($_GET['article_id']) && trim($_GET['article_id'])) ? intval($_GET['article_id']) : 0;
		$keyword = (isset($_GET['keyword']) && trim($_GET['keyword'])) ? trim($_GET['keyword']) :'';
		if ($keyword) {
			$where .= "AND (m.name LIKE '%{$keyword}%' OR l.ip LIKE '%{$keyword}%')";
		}
		if($article_id){
			$where .= "AND (l.article_id LIKE '%{$article_id}%' )";
		}
		$this->setpages(array());
		$sql = "SELECT l.*,m.name FROM jf_article_like l LEFT JOIN jf_member m ON l.member_id = m.id  
				WHERE 1 {$where} ORDER BY l.id DESC";
		$val = $this->ezr->get_results($sql);
		$nav = $this->ezr->get_navigation();

		$this->smarty->assign('val',$val);
		$this->smarty->assign('page',$nav);
		$this->smarty->assign('keyword',$keyword);
		$this->smarty->assign('article_id',$article_id);
		$this->display();
	}
	//delete
	public function delete_like() {
		$id = (isset($_GET['article_id']) && trim($_GET['article_id'])) ? intval($_GET['article_id']) : 0;
		$all = (isset($_GET['all']) && trim($_GET['all'])) ? intval($_GET['all']) : 0;
		$like = (isset($_GET['like']) && trim($_GET['like'])) ? intval($_GET['like']) : 0;
		if($all == 1 && $id > 0){
			$sql = "DELETE FROM jf_article_like WHERE article_id = {$id}";
			if($this->db->query($sql)){
				$sql = "UPDATE jf_article SET likes = 0 WHERE id = {$id}";
				$this->db->query($sql);
			}else{
				echo "删除失败！";
			}
		}
		if($like > 0){
			$sql = "DELETE FROM jf_article_like WHERE id='{$like}'";
			if($this->db->query($sql)){
				$sql = "UPDATE jf_article SET likes = likes-1 WHERE id='{$id}'";
				$this->db->query($sql);
			}else{
				echo "删除失败！";
			}
			
		}
		header("Location:?app=article&act=likes&article_id={$id}");
	}
	//commemt
	public function comment(){
		$where = '';
		$article_id = (isset($_GET['article_id']) && trim($_GET['article_id'])) ? intval($_GET['article_id']) : 0;
		$keyword = (isset($_GET['keyword']) && trim($_GET['keyword'])) ? trim($_GET['keyword']) :'';
		if ($keyword) {
			$where .= "AND (m.name LIKE '%{$keyword}%' OR l.ip LIKE '%{$keyword}%')";
		}
		if($article_id){
			$where .= "AND (l.article_id LIKE '%{$article_id}%' )";
		}
		$this->setpages(array());
		$sql = "SELECT l.*,m.name FROM jf_article_comment l LEFT JOIN jf_member m ON l.member_id = m.id  
				WHERE 1 {$where} ORDER BY l.id DESC";
		$val = $this->ezr->get_results($sql);
		$nav = $this->ezr->get_navigation();

		$this->smarty->assign('val',$val);
		$this->smarty->assign('page',$nav);
		$this->smarty->assign('keyword',$keyword);
		$this->smarty->assign('article_id',$article_id);
		$this->display();
	}
	//comment
	public function delete_comment() {
		$id = (isset($_GET['article_id']) && trim($_GET['article_id'])) ? intval($_GET['article_id']) : 0;
		$all = (isset($_GET['all']) && trim($_GET['all'])) ? intval($_GET['all']) : 0;
		$comment = (isset($_GET['comment']) && trim($_GET['comment'])) ? intval($_GET['comment']) : 0;
		if($all == 1 && $id > 0){
			$sql = "DELETE FROM jf_article_comment WHERE article_id = {$id}";
			if($this->db->query($sql)){
				$sql = "UPDATE jf_article SET comments = 0 WHERE id = {$id}";
				$this->db->query($sql);
			}else{
				echo "删除失败！";
			}
		}
		if($comment > 0){
			$sql = "DELETE FROM jf_article_comment WHERE id='{$comment}'";
			if($this->db->query($sql)){
				$sql = "UPDATE jf_article SET comments = comments-1 WHERE id='{$id}'";
				$this->db->query($sql);
			}else{
				echo "删除失败！";
			}
			
		}
		header("Location:?app=article&act=comment&article_id={$id}");
	}
}
